package com.weishi.project.training.controller;

import java.util.Date;
import java.util.List;

import com.weishi.project.training.domain.Sportsman;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.weishi.framework.aspectj.lang.annotation.Log;
import com.weishi.framework.aspectj.lang.enums.BusinessType;

import java.util.Arrays;

import com.weishi.project.training.domain.SportsmanMatch;
import com.weishi.project.training.service.ISportsmanMatchService;
import com.weishi.framework.web.controller.BaseController;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.common.utils.poi.ExcelUtil;
import com.weishi.framework.web.page.TableDataInfo;

/**
 * sportsmanMatchController
 */
@Api(value = "运动员比赛", description = "运动员比赛")
@RestController
@RequestMapping("/training/sportsmanMatch")
public class SportsmanMatchController extends BaseController {

    @Autowired
    private ISportsmanMatchService sportsmanMatchService;

    /**
     * 查询运动员比赛列表
     */
    @PreAuthorize("@ss.hasPermi('training:sportsmanMatch:list')")
    @GetMapping("/list")
    public TableDataInfo list(SportsmanMatch sportsmanMatch) {
        startPage();
        List<SportsmanMatch> list = sportsmanMatchService.selectSportsmanMatchList(sportsmanMatch);
        return getDataTable(list);
    }

    /**
     * 导出运动员比赛列表
     */
    @PreAuthorize("@ss.hasPermi('training:sportsmanMatch:export')")
    @Log(title = "sportsmanMatch", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SportsmanMatch sportsmanMatch) {
        List<SportsmanMatch> list = sportsmanMatchService.selectSportsmanMatchList(sportsmanMatch);
        ExcelUtil<SportsmanMatch> util = new ExcelUtil<SportsmanMatch>(SportsmanMatch.class);
        return util.exportExcel(list, "sportsmanMatch");
    }

    /**
     * 获取运动员比赛详细信息
     */
    @PreAuthorize("@ss.hasPermi('training:sportsmanMatch:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        System.out.println(sportsmanMatchService.getSportsmanMatchById(id));
        return AjaxResult.success(sportsmanMatchService.getSportsmanMatchById(id));
    }

    /**
     * 新增运动员比赛
     */
    @PreAuthorize("@ss.hasPermi('training:sportsmanMatch:add')")
    @Log(title = "sportsmanMatch", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SportsmanMatch sportsmanMatch) {
        sportsmanMatch.setCtime(new Date());
        sportsmanMatchService.save(sportsmanMatch);
        return AjaxResult.success();
    }

    /**
     * 修改运动员比赛
     */
    @PreAuthorize("@ss.hasPermi('training:sportsmanMatch:edit')")
    @Log(title = "sportsmanMatch", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SportsmanMatch sportsmanMatch) {
        if (sportsmanMatch.getImgUrl() != null) {
            String url = sportsmanMatch.getImgUrl();
            url = url.replace("nullhttp", "http");
            sportsmanMatch.setImgUrl(url);
        }
        sportsmanMatchService.updateSportsmanMatchById(sportsmanMatch);
        return AjaxResult.success();
    }

    /**
     * 删除运动员比赛
     */
    @PreAuthorize("@ss.hasPermi('training:sportsmanMatch:remove')")
    @Log(title = "sportsmanMatch", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        sportsmanMatchService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

    /**
     * 查询运动员姓名集合
     *
     * @return List<String>
     */
    @PreAuthorize("@ss.hasPermi('training:sportsmanMatch:list')")
    @GetMapping("/queryNameList")
    public TableDataInfo querySportsNameList() {
        startPage();
        List<Sportsman> list = sportsmanMatchService.querySportsNameList();
        return getDataTable(list);
    }

}